package com.snailvr.tv.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.util.Util;
import com.snail.readyvr.R;
import com.snailvr.tv.ui.data.SnailMessage;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2017/2/12.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
    private Context mContext;
    private List<SnailMessage> mMessages;

    public MessageAdapter(Context _context, List<SnailMessage> messages) {
        mContext = _context;
        mMessages = messages;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View _view = LayoutInflater.from(mContext).inflate(R.layout.layout_message_item, parent, false);
        final ViewHolder mHolder = new ViewHolder(_view);

        return mHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SnailMessage _Message = mMessages.get(position);
        String _icon = _Message.getIcon();
        Integer r;

        if(_icon.equals("system1")) {
            r = R.drawable.protrait_1;
            holder.mImageViewIcon.setImageResource(r);

        } else if (_icon.equals("system2")) {
            r = R.drawable.protrait_2;
            holder.mImageViewIcon.setImageResource(r);

        } else if (_icon.equals("system3")) {
            r = R.drawable.protrait_3;
            holder.mImageViewIcon.setImageResource(r);

        } else if (_icon.equals("system4")) {
            r = R.drawable.protrait_4;
            holder.mImageViewIcon.setImageResource(r);

        } else if (_icon.equals("system5")) {
            r = R.drawable.protrait_5;
            holder.mImageViewIcon.setImageResource(r);

        } else if (_icon.equals("system6")) {
            r = R.drawable.protrait_6;
            holder.mImageViewIcon.setImageResource(r);

        } else if (_icon.startsWith("http://")){
            if (Util.isOnMainThread()){
                Glide.with(mContext)
                        .load(_Message.getIcon())
                        .diskCacheStrategy(DiskCacheStrategy.RESULT)
                        .into(holder.mImageViewIcon);
            }
        }

        holder.mTextViewNickName.setText(_Message.getNickName());
        holder.mTextViewMessage.setText(Html.fromHtml(_Message.getMessage(),getImageGetterInstance(),null));
    }

    @Override
    public int getItemCount() {
        return mMessages.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder
    {

        @BindView(R.id.id_text_view_nickname)
        TextView mTextViewNickName;
        @BindView(R.id.id_text_view_message)
        TextView mTextViewMessage;
        @BindView(R.id.roundImage_view_icon)
        ImageView mImageViewIcon;
        public ViewHolder(View _view) {
            super(_view);
            ButterKnife.bind(this, _view);
        }
    }

    /**
     * ImageGetter用于text图文混排
     *
     * @return
     */
    public ImageGetter getImageGetterInstance() {
        ImageGetter imgGetter = new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                int fontH = (int) (mContext.getResources().getDimension(
                        R.dimen.font_size_12sp) * 1.5);
                int id = Integer.parseInt(source);
                Drawable d = mContext.getResources().getDrawable(id);
                int height = fontH;
                int width = (int) ((float) d.getIntrinsicWidth() / (float) d
                        .getIntrinsicHeight()) * fontH;
                if (width == 0) {
                    width = d.getIntrinsicWidth();
                }
                d.setBounds(0, 0, width, height);
                return d;
            }
        };
        return imgGetter;
    }
}
